package com.nexgo.payment.trans.scanvoid;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.nexgo.payment.R;
import com.nexgo.payment.action.confirmtransdetail.ConfirmTransDetailFragment;
import com.nexgo.payment.action.inputadminpwd.AdminPasswordFragment;
import com.nexgo.payment.action.inputtracecode.InputTraceCodeFragment;
import com.nexgo.payment.action.scan.ScanFragment;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.util.ActivityUtils;

/**
 * 扫码撤销
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月13日
 */
public class ScanVoidActivity extends AbsTransActivity<ScanVoidContract.Presenter> implements ScanVoidContract.View{
    private static final String TRACE_CODE = "trace_code";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        String traceCode = null;
        if (bundle != null){
            traceCode = bundle.getString(TRACE_CODE, "");
        }
        new ScanVoidPresenter(this, Repository.getInstance(), traceCode);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
        //第一次调用不起作用，扫码返回后再次调用才起作用
        mPresenter.showOnScanNextAndRestart();
    }

    @Override
    public void showInputAdminPassword() {
        AdminPasswordFragment adminPwdFragment = AdminPasswordFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showInputTraceCode() {
        InputTraceCodeFragment traceCodeFragment = InputTraceCodeFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), traceCodeFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), traceCodeFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showScanFragment() {
        ScanFragment scanFragment = ScanFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showConfirmTransDetail(String traceCode) {
        ConfirmTransDetailFragment transDetailFragment = ConfirmTransDetailFragment.newInstance(traceCode);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), transDetailFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), transDetailFragment, R.id.contentFrame);
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, ScanVoidActivity.class);
    }

    public static Intent newInstance(Context context, String traceCode) {
        Intent intent = new Intent(context, ScanVoidActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(TRACE_CODE, traceCode);
        intent.putExtras(bundle);
        return intent;
    }
}
